<?php

class qianyi {

    # 本人信息
    public $member = array();

    # 下线人数
    public $memberOne = array();
    public $memberTwo = array();
    public $memberThr = array();

    # 订单信息
    public $orderOne = array();
    public $orderTwo = array();
    public $orderThr = array();

    # 订单商品（佣金）信息
    public $orderGoodsOne = array();
    public $orderGoodsTwo = array();
    public $orderGoodsThr = array();

    # 佣金
    public $commissionOne = array();
    public $commissionTwo = array();
    public $commissionThr = array();

    public function getMemberInfo($agentid){

        # 会员信息
        $memberOne = $memberTwo = $memberThr = array();
        # 订单信息
        $orderOne = $orderTwo = $orderThr = array();
        # 订单商品信息
        $orderGoodsOne = $orderGoodsTwo = $orderGoodsThr = array();

        # 一级会员信息
        $memberOneSql = 'SELECT id, nickname, agentid, oldagentid, upleveltime, oldlevel FROM ' . tablename('ewei_shop_member') . ' where (`agentid` = :agentid OR `oldagentid` = :agentid) AND `isagent` = 1 AND `status` = 1;';
        $memberOne = pdo_fetchall($memberOneSql, array( ':agentid' => $agentid ), 'id');

        # 一级订单信息
        $orderOneSql = 'SELECT * FROM ' . tablename('ewei_shop_order') . ' where `agentid` = :agentid AND `status` >= 0 ORDER BY `id` ASC;';
        $orderOne = pdo_fetchall($orderOneSql, array( ':agentid' => $agentid ), 'id');

        if (!empty( $orderOne )) {
            # 一级订单商品（佣金）信息
            $orderGoodsOneSql = 'SELECT * FROM ' . tablename('ewei_shop_order_goods') . ' WHERE `orderid` IN ( ' . implode(',', array_keys($orderOne)) . ' ) ';
            $orderGoodsOne = pdo_fetchall($orderGoodsOneSql, array(), 'id');
        }

        if (!empty( $memberOne )) {
            # 二级订单信息
            $orderTwoSql = 'SELECT * FROM ' . tablename('ewei_shop_order') . ' where `agentid` IN ( ' . implode(',', array_keys($memberOne)) . ' ) AND `status` >= 0 ORDER BY `id` ASC ;';
            $orderTwo = pdo_fetchall($orderTwoSql, array(), 'id');

            if (!empty( $orderTwo )) {
                # 二级订单商品（佣金）信息
                $orderGoodsTwoSql = 'SELECT * FROM ' . tablename('ewei_shop_order_goods') . ' WHERE `orderid` IN ( ' . implode(',', array_keys($orderTwo)) . ' ) ';
                $orderGoodsTwo = pdo_fetchall($orderGoodsTwoSql, array(), 'id');
            }

            # 二级会员信息
            $memberTwoSql = 'SELECT id, nickname, agentid, oldagentid, upleveltime, oldlevel FROM ' . tablename('ewei_shop_member') . ' where ( `agentid` in (' . implode(',', array_keys($memberOne)) . ') OR `oldagentid` in (' . implode(',', array_keys($memberOne)) . ') ) AND `isagent` = 1 AND `status` = 1;';
            $memberTwo = pdo_fetchall($memberTwoSql, array(), 'id');

            if (!empty( $memberTwo )) {
                # 三级订单信息
                $orderThrSql = 'SELECT * FROM ' . tablename('ewei_shop_order') . ' where `agentid` IN ( ' . implode(',', array_keys($memberTwo)) . ' ) AND `status` >= 0 ORDER BY `id` ASC;';
                $orderThr = pdo_fetchall($orderThrSql, array(), 'id');

                if (!empty( $orderThr )) {
                    # 三级订单商品（佣金）信息
                    $orderGoodsThrSql = 'SELECT * FROM ' . tablename('ewei_shop_order_goods') . ' WHERE `orderid` IN ( ' . implode(',', array_keys($orderThr)) . ' ) ';
                    $orderGoodsThr = pdo_fetchall($orderGoodsThrSql, array(), 'id');
                }

                # 三级会员信息
                $memberThrSql = 'SELECT id, nickname, agentid, oldagentid, upleveltime, oldlevel FROM ' . tablename('ewei_shop_member') . ' where ( `agentid` in (' . implode(',', array_keys($memberTwo)) . ') OR `oldagentid` in (' . implode(',', array_keys($memberTwo)) . ') ) AND `isagent` = 1 AND `status` = 1;';
                $memberThr = pdo_fetchall($memberThrSql, array(), 'id');
            }
        }

        $this->member = m('member')->getMember($agentid);

        $this->memberOne = $memberOne;
        $this->memberTwo = $memberTwo;
        $this->memberThr = $memberThr;

        $this->orderOne = $orderOne;
        $this->orderTwo = $orderTwo;
        $this->orderThr = $orderThr;

        $this->orderGoodsOne = $orderGoodsOne;
        $this->orderGoodsTwo = $orderGoodsTwo;
        $this->orderGoodsThr = $orderGoodsThr;

        return $this;
    }

    public function getRecord(){
        global $_W;

        if (empty( $this->member )) {
            return false;
        }


        if (!empty( $this->orderGoodsOne )) {

            $res = array();

            foreach ($this->orderGoodsOne as $item) {

                $order = $this->orderOne[$item['orderid']];
                $member = $this->member;
                $commission = iunserializer($item['commission1']);

                $agentlevel = $member['agentlevel'];

                if (!empty( $member['oldlevel'] ) && $order['createtime'] < $member['upleveltime']) {
                    $agentlevel = $member['oldlevel'];
                }

                $commissionPrice = isset( $commission ['level' . $agentlevel] ) ? $commission ['level' . $agentlevel] : $commission ['default'];

                if (empty( $commissionPrice )) {
                    continue;
                }

                # record
                $temp['uniacid'] = $_W['uniacid'];
                $temp['mid'] = $this->member['id'];

                $temp['orderid'] = $item['orderid'];
                $temp['goods_id'] = $item['goodsid'];
                $temp['cg_money'] = $item['price'];

                $temp['c_money'] = $commissionPrice;

                $temp['o_create_time'] = $order['createtime'];
                $temp['o_send_time'] = $order['sendtime'];
                $temp['o_pay_time'] = $order['paytime'];
                $temp['o_finish_time'] = $order['finishtime'];
                $temp['o_status'] = $order['status'];

                $temp['c_rank']  = 1;
                $temp['c_type']  = 1;
                $temp['c_rate']  = 0;

                $i = $temp['c_rank'];

                $temp['c_apply_time'] = $item['applytime'.$i];
                $temp['c_check_time'] = $item['checktime'.$i];
                $temp['c_pay_time'] = $item['paytime'.$i];
                $temp['c_invalid_time'] = $item['invalidtime'.$i];
                $temp['c_content'] = $item['content'.$i];
                $temp['c_status'] = $item['status'.$i];

                /*
                $temp['c_money'] = $commissionPrice;

                $temp['commission'] = $commission;
                $temp['commissionPrice'] = $commissionPrice;
                $temp['agentlevel'] = $agentlevel;
                $temp['order'] = $order['id'];

                if($order['status'] >= 3 && $item['status1']){
                    $temp['canPrice'] = $commissionPrice;
                }*/

                $res[] = $temp;
            }

            unset( $item );
            $this->commissionOne = $res;

        }

        if (!empty( $this->orderGoodsTwo )) {

            $res = array();

            foreach ($this->orderGoodsTwo as $item) {

                $order = $this->orderTwo[$item['orderid']];
                $member = $this->member;
                $memberCurrent = $this->memberOne[$item['agentid']];
                $commission = iunserializer($item['commission2']);

                $agentlevel = $member['agentlevel'];

                $can = false;

                # 当前用户为特使情况下，需要订单时间，是否与本人存在关系。
                if (!empty( $memberCurrent['oldagentid'] )) {

                    if($memberCurrent['upleveltime'] < $order['createtime']){
                        $can = true;
                    }

                } else {
                    # 当前用户为非特使情况下，只需要考虑当前用户等级问题。
                    $can = true;
                }

                if($can){

                    if (!empty( $member['oldlevel'] ) && $order['createtime'] < $member['upleveltime']) {
                        $agentlevel = $member['oldlevel'];
                    }

                    $commissionPrice = isset( $commission ['level' . $agentlevel] ) ? $commission ['level' . $agentlevel] : $commission ['default'];

                    if (empty( $commissionPrice )) {
                        continue;
                    }


                    # record
                    $temp['uniacid'] = $_W['uniacid'];
                    $temp['mid'] = $this->member['id'];

                    $temp['orderid'] = $item['orderid'];
                    $temp['goods_id'] = $item['goodsid'];
                    $temp['cg_money'] = $item['price'];

                    $temp['c_money'] = $commissionPrice;

                    $temp['o_create_time'] = $order['createtime'];
                    $temp['o_send_time'] = $order['sendtime'];
                    $temp['o_pay_time'] = $order['paytime'];
                    $temp['o_finish_time'] = $order['finishtime'];
                    $temp['o_status'] = $order['status'];

                    $temp['c_rank']  = 2;
                    $temp['c_type']  = 1;
                    $temp['c_rate']  = 0;

                    $i = $temp['c_rank'];

                    $temp['c_apply_time'] = $item['applytime'.$i];
                    $temp['c_check_time'] = $item['checktime'.$i];
                    $temp['c_pay_time'] = $item['paytime'.$i];
                    $temp['c_invalid_time'] = $item['invalidtime'.$i];
                    $temp['c_content'] = $item['content'.$i];
                    $temp['c_status'] = $item['status'.$i];

                    $res[] = $temp;

                }

            }

            unset( $item );
            $this->commissionTwo = $res;
        }

        if (!empty( $this->orderGoodsThr )) {

            $res = array();

            foreach ($this->orderGoodsThr as $item) {

                $order = $this->orderThr[$item['orderid']];

                $member = $this->member;
                $memberCurrent = $this->memberTwo[$item['agentid']];
                $memberAgent = $this->memberOne[$memberCurrent['agentid']];

                $can = false;

                if (empty( $memberCurrent['oldagentid'] )) {

                    if (empty( $memberAgent['oldagentid'] )) {
                        $can = true;
                    } else {
                        # 当前用户上级为特使情况下，需要订单时间判断，是否与上级存在关系。
                        if ($memberAgent['upleveltime'] < $order['createtime']) {
                            $can = true;
                        }
                    }

                } else {
                    # 当前用户为特使情况下，需要订单时间判断，是否与本人存在关系。
                    if($memberCurrent['upleveltime'] < $order['createtime']){

                        if (empty( $memberAgent['oldagentid'] )) {
                            $can = true;
                        } else {
                            # 当前用户上级为特使情况下，需要订单时间判断，是否与上级存在关系。
                            if ($memberAgent['upleveltime'] < $order['createtime']) {
                                $can = true;
                            }
                        }
                    }
                }

                if($can){

                    $commission = iunserializer($item['commission3']);
                    $agentlevel = $member['agentlevel'];

                    if (!empty( $member['oldlevel'] ) && $order['createtime'] < $member['upleveltime']) {
                        $agentlevel = $member['oldlevel'];
                    }

                    $commissionPrice = isset( $commission ['level' . $agentlevel] ) ? $commission ['level' . $agentlevel] : $commission ['default'];

                    if (empty( $commissionPrice )) {
                        continue;
                    }

                    # record
                    $temp['uniacid'] = $_W['uniacid'];
                    $temp['mid'] = $this->member['id'];

                    $temp['orderid'] = $item['orderid'];
                    $temp['goods_id'] = $item['goodsid'];
                    $temp['cg_money'] = $item['price'];

                    $temp['c_money'] = $commissionPrice;

                    $temp['o_create_time'] = $order['createtime'];
                    $temp['o_send_time'] = $order['sendtime'];
                    $temp['o_pay_time'] = $order['paytime'];
                    $temp['o_finish_time'] = $order['finishtime'];
                    $temp['o_status'] = $order['status'];

                    $temp['c_rank']  = 3;
                    $temp['c_type']  = 1;
                    $temp['c_rate']  = 0;

                    $i = $temp['c_rank'];

                    $temp['c_apply_time'] = $item['applytime'.$i];
                    $temp['c_check_time'] = $item['checktime'.$i];
                    $temp['c_pay_time'] = $item['paytime'.$i];
                    $temp['c_invalid_time'] = $item['invalidtime'.$i];
                    $temp['c_content'] = $item['content'.$i];
                    $temp['c_status'] = $item['status'.$i];

                    $res[] = $temp;
                }

            }

            unset( $item );
            $this->commissionThr = $res;
        }

        return array( 'one' => $this->commissionOne, 'two' => $this->commissionTwo, 'thr' => $this->commissionThr, );
    }


}